<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>个人主页</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            margin: 0;
            padding: 0;
        }

        .profile-container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
            background-color: #fff;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }

        .profile-header {
            display: flex;
            align-items: center;
            margin-bottom: 20px;
        }

        .profile-info {
            flex: 1;
        }

        .profile-name {
            margin: 0;
            font-size: 24px;
            color: #333;
        }

        .profile-bio {
            margin: 10px 0;
            color: #666;
        }

        .stat-item {
            margin-right: 20px;
            text-align: center;
        }

        .stat-number {
            font-size: 18px;
            font-weight: bold;
            color: #333;
        }

        .stat-label {
            font-size: 14px;
            color: #666;
        }

        .profile-content {
            margin-top: 20px;
        }

        .video-list {
            display: auto;
            flex-wrap: wrap;
            gap: 10px;
        }

        .video-item {
            width: calc(33.333% - 10px);
            background-color: #f9f9f9;
            padding: 10px;
            box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
            text-align: center;
        }

        .video-item img {
            width: 100%;
            height: auto;
            border-radius: 5px;
        }

        .video-item p {
            margin: 10px 0 0;
            font-size: 16px;
            color: #333;
        }

        .edit-profile-btn {
            background-color: #007bff;
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            transition: background-color 0.3s;
        }

        .edit-profile-btn:hover {
            background-color: #0056b3;
        }

        .modal {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.5);
            z-index: 1000;
        }

        .modal-content {
            background-color: white;
            width: 400px;
            padding: 20px;
            border-radius: 8px;
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
        }

        .modal-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            border-bottom: 1px solid #ddd;
            padding-bottom: 10px;
            margin-bottom: 10px;
        }

        .close-btn {
            font-size: 24px;
            cursor: pointer;
            color: #666;
        }

        .form-group {
            margin-bottom: 15px;
        }

        .form-group label {
            display: block;
            margin-bottom: 5px;
            font-weight: 500;
        }

        .form-group input {
            width: 100%;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }

        .submit-btn {
            background-color: #28a745;
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            float: right;
        }
        .return-btn {
            background-color: #6c757d;
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            transition: background-color 0.3s;
            margin-bottom: 20px;
        }

        .return-btn:hover {
            background-color: #5a6268;
        }
    </style>
</head>
<body>
    <!-- 返回按钮 -->
    <div class="profile-container">
        <button class="return-btn" onclick="goBack()">返回</button>
    </div>
    <div class="profile-container">
        <div class="profile-header">
            <div class="profile-info">
                <h1 class="profile-name">昵称</h1>
                <p class="profile-bio">简介：这里是一段简短的自我介绍。</p>
            </div>
            <button class="edit-profile-btn" onclick="openEditModal()">修改个人信息</button>
        </div>
        <div class="profile-content">
            <h2>我的收藏夹</h2>
            <div class="video-list">
                <div class="video-item">
                    <p>收藏夹1</p>
                </div>
                <div class="video-item">
                    <p>收藏夹2</p>
                </div>
                <div class="video-item">
                    <p>收藏夹3</p>
                </div>
            </div>
        </div>
        <div class="profile-content">
            <h2>我的喜欢</h2>
            <div class="video-list">
                <div class="video-item">
                    <p>喜欢1</p>
                </div>
                <div class="video-item">
                    <p>喜欢2</p>
                </div>
                <div class="video-item">
                    <p>喜欢3</p>
                </div>
            </div>
        </div>
    </div>

    <!-- 修改个人信息弹窗 -->
    <div id="editModal" class="modal">
        <div class="modal-content">
            <div class="modal-header">
                <h3>修改个人信息</h3>
                <span class="close-btn" onclick="closeEditModal()">&times;</span>
            </div>
            <form id="editProfileForm" onsubmit="submitEditForm(event)">
                <div class="form-group">
                    <label for="username">用户名</label>
                    <input type="text" id="username" >
                </div>
                <div class="form-group">
                    <label for="phone_number">手机号</label>
                    <input type="text" id="phone_number" >
                </div>
                <div class="form-group">
                    <label for="password">密码</label>
                    <input type="password" id="password" >
                </div>
                <button type="submit" class="submit-btn">保存</button>
            </form>
        </div>
    </div>

    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
    <script>

        // 返回按钮
        function goBack() {
            window.history.back();
        }
        
        // 打开修改个人信息弹窗
        function openEditModal() {
            document.getElementById('editModal').style.display = 'block';
        }

        // 关闭修改个人信息弹窗
        function closeEditModal() {
            document.getElementById('editModal').style.display = 'none';
        }

        // 提交修改表单
        function submitEditForm(event) {
            event.preventDefault();

            const username = document.getElementById('username').value.trim();
            const phone_number = document.getElementById('phone_number').value.trim();
            const password = document.getElementById('password').value.trim();

            const user_id = 8; // 用户ID暂时为8

            // 构建请求数据，只包含用户填写的内容
            const data = {};
            if (username) data.username = username;
            if (phone_number) data.phone_number = phone_number;
            if (password) data.password = password;

            // 如果没有填写任何内容，提示用户
            if (Object.keys(data).length === 0) {
                alert('请至少填写一项内容');
                return;
            }

            axios({
                method: 'put',
                url: `http://127.0.0.1:5000/admin/users/${user_id}`,
                headers: {
                    'Content-Type': 'application/json',
                },
                data: data
            })
            .then(function (response) {
                if (response.data) {
                    alert('用户信息更新成功！');
                    closeEditModal();
                } else {
                    alert('用户信息更新失败：' + response.data.message);
                    closeEditModal();
                }
            })
            .catch(function (error) {
                console.log('更新用户信息失败：', error);
                if (error.response) {
                    alert('更新用户信息失败：' + (error.response.data.error || '服务器错误'));
                } else {
                    alert('更新用户信息失败：网络错误，请检查连接');
                }
            });
        }

        // 点击模态框外部关闭
        window.onclick = function(event) {
            const modal = document.getElementById('editModal');
            if (event.target === modal) {
                closeEditModal();
            }
        }
            
    </script>
</body>
</html>